import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 62536
 * Date: 2024-02-22
 * Time: 8:55
 */
public class Test {

    //自底向上的层序遍历（即按从叶子节点所在层到根节点所在的层，逐层从左向右遍历）
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> retList = new ArrayList<>();
        if(root == null) {
            return retList;
        }
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        int len = 0;
        while(!qu.isEmpty()) {
            int size = qu.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode cur = qu.poll();
                list.add(cur.val);
                size--;
                if(cur.left != null) {
                    qu.offer(cur.left);
                }
                if(cur.right != null) {
                    qu.offer(cur.right);
                }
            }
            retList.add(list);
            len++;
        }
        List<List<Integer>> retList2 = new ArrayList<>();
        for(int i = len - 1; i >= 0; i--){
            retList2.add(retList.get(i));
        }


        return retList2;

    }

    //二叉树层序遍历
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> retList = new ArrayList<>();
        if(root == null) {
            return retList;
        }
        Queue<TreeNode> qu = new LinkedList<>();
        qu.offer(root);
        while(!qu.isEmpty()) {
            int size = qu.size();
            List<Integer> list = new ArrayList<>();
            while(size != 0) {
                TreeNode cur = qu.poll();
                list.add(cur.val);
                size--;
                if(cur.left != null) {
                    qu.offer(cur.left);
                }
                if(cur.right != null) {
                    qu.offer(cur.right);
                }
            }

            retList.add(list);

        }


        return retList;
    }

    //二叉树的构建和遍历
    //编一个程序，读入用户输入的一串先序遍历字符串，根据此字符串建立一个二叉树，建立起此二叉树以后，再对二叉树进行中序遍历，输出遍历结果。
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        String str = in.nextLine();

        TreeNode node = createTree(str);
        inOrder(node);
    }

    public static int i = 0;
    private static TreeNode createTree(String str){
        TreeNode root = null;
        char ch = str.charAt(i);
        if(ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            i++;
        }
        return root;
    }

    //中序遍历
    private static void inOrder (TreeNode root){
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val +" ");
        inOrder(root.right);
    }
}
